<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateQrcodeRequest;
use App\Http\Requests\UpdateQrcodeRequest;
use App\Models\Qrcode;
use App\Repositories\ConfigRepository;
use App\Repositories\QrcodeRepository;
use App\Http\Controllers\AppBaseController;
use App\User;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class QrcodeController extends AppBaseController
{
    /** @var  QrcodeRepository */
    private $qrcodeRepository;
    public $configRepository;

    public function __construct(QrcodeRepository $qrcodeRepo, ConfigRepository $configRepository)
    {
        $this->configRepository = $configRepository;
        $this->qrcodeRepository = $qrcodeRepo;
    }

    /**
     * Display a listing of the Qrcode.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $shop = Auth::guard('admin')->user()->shop;

        $qrcodes = Qrcode::where('shop_id',$shop->id)->get();

        return view('qrcodes.index')
            ->with('qrcodes', $qrcodes);
    }

    /**
     * Show the form for creating a new Qrcode.
     *
     * @return Response
     */
    public function create()
    {
        return view('qrcodes.create');
    }

    /**
     * Store a newly created Qrcode in storage.
     *
     * @param CreateQrcodeRequest $request
     *
     * @return Response
     */
    public function store(CreateQrcodeRequest $request)
    {
        $input = $request->all();

        $shop = Auth::guard('admin')->user()->shop;
        if($shop)
            $input['shop_id'] = $shop->id;

        $qrcode = $this->qrcodeRepository->create($input);

        if($input['type']==1){
            $qrcode_pic_content = \SimpleSoftwareIO\QrCode\Facades\QrCode::format('png')->size(500)->generate(route("wechat.login",['qrcode_id'=>$qrcode->id]));

            $path = '/qrcodes/qrcode_'.$qrcode->id.'.png';
            $ok = Storage::disk('oss')->put($path, $qrcode_pic_content);
            if($ok){
                $url = config('app.pic_oss_url_domain').$path;
                $qrcode->pic_url = $url;
                $qrcode->save();
            }else{
            }
        }elseif ($input['type']==2){
            $this->configRepository->getWechatConfig(Auth::guard('admin')->user()->shop->wechat->id);

            $result = $this->configRepository->app->qrcode->forever($input['url']);
            $ticket = $result['ticket'];
            $url = $this->configRepository->app->qrcode->url($ticket);
            $qrcode->pic_url = $url;
            $qrcode->save();
        }
        Flash::success('Qrcode saved successfully.');

        return redirect(route('qrcodes.index'));
    }

    /**
     * Display the specified Qrcode.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            Flash::error('Qrcode not found');

            return redirect(route('qrcodes.index'));
        }
        $model = $qrcode->scanLogs()->orderBy('created_at');
        $nickname = \request('nickname');
        if($nickname){
            $user_ids = User::where('nickname','like',"%$nickname%")->pluck('id')->toArray();
            $model = $model->whereIn('user_id',$user_ids);
        }
        $count = $model->count();
        $logs = $model->paginate(20);
        return view('qrcodes.show')->with(compact('logs','qrcode','count'));
    }

    /**
     * Show the form for editing the specified Qrcode.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            Flash::error('Qrcode not found');

            return redirect(route('qrcodes.index'));
        }

        return view('qrcodes.edit')->with('qrcode', $qrcode);
    }

    /**
     * Update the specified Qrcode in storage.
     *
     * @param  int              $id
     * @param UpdateQrcodeRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateQrcodeRequest $request)
    {
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            Flash::error('Qrcode not found');

            return redirect(route('qrcodes.index'));
        }

        $qrcode = $this->qrcodeRepository->update($request->all(), $id);

        $qrcode_pic_content = \SimpleSoftwareIO\QrCode\Facades\QrCode::format('png')->size(500)->generate(route("wechat.login",['qrcode_id'=>$qrcode->id]));

        $path = '/qrcodes/qrcode_'.$qrcode->id.'.png';
        $ok = Storage::disk('oss')->put($path, $qrcode_pic_content);
        if($ok){
            $url = config('app.pic_oss_url_domain').$path;
            $qrcode->pic_url = $url;
            $qrcode->save();
        }else{
        }

        Flash::success('Qrcode updated successfully.');

        return redirect(route('qrcodes.index'));
    }

    /**
     * Remove the specified Qrcode from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            Flash::error('Qrcode not found');

            return redirect(route('qrcodes.index'));
        }

        $this->qrcodeRepository->delete($id);

        Flash::success('Qrcode deleted successfully.');

        return redirect(route('qrcodes.index'));
    }
}
